# Definition for singly-linked list.
from typing import Optional


class ListNode:

    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:

    def addTwoNumbers(self, l1: Optional[ListNode],
                      l2: Optional[ListNode]) -> Optional[ListNode]:

        def reverse_linked_list(l: Optional[ListNode]) -> ListNode:
            if l is None:
                return None
            root, node = None, l
            while node is not None:
                tmp = node.next
                node.next = root
                root = node
                node = tmp
            return root

        r1 = reverse_linked_list(l1)
        r2 = reverse_linked_list(l2)
        carry = 0
        node = root = ListNode()
        while r1 is not None or r2 is not None or carry > 0:
            val = carry
            if r1 is not None:
                val += r1.val
                r1 = r1.next
            if r2 is not None:
                val += r2.val
                r2 = r2.next
            node.next = ListNode(val=val % 10)
            carry = val // 10
            node = node.next
                
        return reverse_linked_list(root.next)


so = Solution()
root = ListNode()
node = root
for num in [7, 2, 4, 3]:
    node.next = ListNode(val=num)
    node = node.next


def copy_linked_list(l: Optional[ListNode]) -> Optional[ListNode]:
    node = root = ListNode()
    while l is not None:
        node.next = ListNode(val=l.val)
        l = l.next
        node = node.next
    return root.next


def print_linked_list(l: Optional[ListNode]):
    a = []
    while l is not None:
        a.append(l.val)
        l = l.next
    print(a)


def reverse_linked_list(l: Optional[ListNode]) -> ListNode:
    if l is None:
        return None
    root, node = None, l
    while node is not None:
        tmp = node.next
        node.next = root
        root = node
        node = tmp
    return root


a = []
l = so.addTwoNumbers(root.next, copy_linked_list(root.next))

while l is not None:
    a.append(l.val)
    l = l.next
print(a)